<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <!-- 页面meta -->
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>电商管理后台</title>
    <link rel="shortcut icon" href="../favicon.ico">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../styles/common.css" />
    <link rel="stylesheet" href="../styles/index.css" />
    <link rel="stylesheet" href="../styles/icon/iconfont.css" />
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="订单编码" v-model="pagination.queryString" style="width: 200px;" class="filter-item" clearable></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column prop="id" label="订单编码" align="center"></el-table-column>
                <el-table-column prop="code" label="订单编号" align="center"></el-table-column>
                <el-table-column prop="userName" label="用户名称" align="center"></el-table-column>
                <el-table-column prop="time" label="创建时间" align="center"></el-table-column>
                <el-table-column prop="addressName" label="订单地址" align="center"></el-table-column>
                <el-table-column label="订单状态" align="center">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status=='0'">未发货</span>
                        <span v-if="scope.row.status=='1'">已发货</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleDetail(scope.row)">查看订单</el-button>
                        <el-button size="mini" type="success" v-if="scope.row.status=='0'" @click="switchStatus(scope.row)">发货</el-button>
                        <el-button size="mini" type="info" v-else disabled>已发货</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="订单详情" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-table :data="tableData" border style="width: 100%">
                            <el-table-column prop="id" label="ID" align="center"></el-table-column>
                            <el-table-column prop="goodsName" label="商品名称" align="center"></el-table-column>
                            <el-table-column prop="number" label="商品数量" align="center"></el-table-column>
                            <el-table-column prop="goodsPrice" label="商品价格" align="center"></el-table-column>
                        </el-table>
                        <span style="float: right" >共{{total}}元</span>
                    </template>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../plugins/axios/axios.min.js"></script>
<script src="../js/request.js"></script>
<script src="../api/login.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data(){
            return{
                pagination: {//分页相关模型数据
                    currentPage: 1,//当前页码
                    pageSize:4,//每页显示的记录数
                    total:0,//总记录数
                    queryString:null//查询条件
                },
                dataList: [],//当前页要展示的分页列表数据
                tableData: [],
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则
                }
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //分页查询
            findPage() {
                var param={
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/orders/pageQuery",param).then((res) => {
                    this.pagination.total=res.data.data.total;
                    this.dataList=res.data.data.records;
                }).catch((r)=> {
                    this.showMessage(r);
                });
            },
            // 重置表单
            resetForm() {
                this.tableData={};
            },
            // 弹出编辑窗口
            handleDetail(row) {
                this.dialogFormVisible4Edit=true;
                axios.get("/orderitem/findAll?id="+row.id).then((res) => {
                    if(res.data.flag){
                        this.tableData=res.data.data;
                        this.total = 0;
                        for (var i = 0;i<this.tableData.length;i++){
                            this.total = this.total + this.tableData[i].goodsPrice*this.tableData[i].number;
                        }
                    }else {
                        this.$message.error(res.data.message);
                    }
                }).catch((r)=> {
                    this.showMessage(r);
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage=currentPage;
                this.findPage();
            },
            showMessage(r){
                if (r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            switchStatus(row) {
                this.$confirm("你确定要发货吗？","提示").then(() => {
                    axios.put("/orders/edit",row).then((res) => {
                        if(res.data.flag){
                            this.$message({
                                type:'success',
                                message:res.data.message
                            });
                            this.findPage();
                        }else {
                            this.$message.error(res.data.message);
                        }
                    }).catch((r)=> {
                        this.showMessage(r);
                    });
                }).catch(() => {
                    this.$message({
                        type:'info',
                        message:"操作已取消"
                    });
                })
            }
        }
    })
</script>
</html>